package servlet.index;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import pojo.Book;
import pojo.Book2;
import service.ConvertBooksType;
import service.SearchIndexpagere;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/indexpage")
public class IndexindexpageServlet extends HttpServlet {
    private ConvertBooksType convertBooksType = new ConvertBooksType();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应对象类型
        resp.setContentType("application/json;charset=utf-8");
        SearchIndexpagere searchIndexpagere = new SearchIndexpagere();
        List<Book> books = searchIndexpagere.selectRandomBook();
        System.out.println(books.get(0).getId());
        List<Book2> book2s = convertBooksType.parseBooks(books);
        String jsonString = JSON.toJSONString(book2s);
        PrintWriter writer = resp.getWriter();
        writer.write(jsonString);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
